package Easy;

import java.util.HashMap;
import java.util.Map;

// 290. 单词规律
public class Solution290 {
    public boolean wordPattern(String pattern, String s) {
        Map<Character, String> map = new HashMap<Character, String>();
        String[] str = s.split(" ", 0);
        if(pattern.length()!=str.length)
            return false;
        for (int i = 0; i < pattern.length(); i++) {
            char c = pattern.charAt(i);
            String s1 = str[i];
            // 如果包含c，且get(c)!=s1 或者map里没有c，但是s1已经存在了，都返回false
            if (map.containsKey(c)) {
                if (!map.get(c).equals(s1))
                    return false;
            } else if (map.containsValue(s1)) {
                return false;
            } else {
                map.put(c, s1);
            }
        }
        return true;
    }
}
